#include	<p18f452.h> 	
#include	<delays.h>		
#include	<adc.h>


#pragma	config	OSC = HS,	OSCS = OFF
#pragma	config	PWRT = ON,	BOR = ON,	BORV = 25
#pragma 	config	WDT = OFF,	WDTPS = 128
#pragma	config	CCP2MUX = OFF,	LVP = ON//programacion bajo voltaje
#pragma	config	STVR = ON//stack overflow
#pragma	config	WRT0 =OFF,	WRT1 = OFF,	WRT2 = OFF
#pragma	config	WRT3 = OFF,	WRTB = OFF
#pragma	config	EBTR0 = OFF,	EBTR1 = OFF,	EBTR2 = OFF
#pragma	config	EBTR3 = OFF,	EBTRB = OFF

void  inicializacion();


#pragma udata access mis_var1 = 0x20	
float POTEN,PID,VALOR;
#pragma udata
 
#pragma	code	high_vector=0x08
#pragma	code



void main(void)							
{
	inicializacion();
	while(1)
	{
		SetChanADC( ADC_CH0 );		//conversion
		Delay10TCYx( 5 ); 
		ConvertADC(); 
		while( BusyADC() ); 
		POTEN= ReadADC()/204.6;
		SetChanADC( ADC_CH1 );
		Delay10TCYx( 5 ); 
		ConvertADC(); 
		while( BusyADC() ); 
		PID=ReadADC()/204.6;
		if(POTEN<4.5 && POTEN>0.5)					// LIMITES PARA LOS FIENES DE CARRERA
		{
			VALOR = PID - POTEN;    	
			if(VALOR<=0.25 && VALOR>=-0.25)                 							  //se coloca 7 para que se enciendan en 1 rb0, rb1,  rb2----motor parado
			{		
				PORTB=0;	
			}
			if(VALOR>0.25 )	
			{      
				PORTB=5;
			}			 
	        if(VALOR< -0.25)	
			{ 
				PORTB=3; 
			}            		
		}
		else
		{
			PORTB=0;
		}	   
			
	}
}
void inicializacion()
{
	RCONbits.IPEN		=	1;
	INTCONbits.GIE		=	1;
	OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_3ANA_0REF, ADC_CH0 & ADC_INT_OFF );
	TRISB=0;
	
}
